//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    // console.log("app - onLaunch");
    // 4. 检查storge
    let that = this
    var openid = ""
    try {
      openid = wx.getStorageSync("openid")

      if (openid) {
        // Do something with return value
        if (openid != "") {
          this.globalData.openid = openid
        }
      }
    } catch (e) {
      // Do something when catch error
    }

    if (openid == "") {

      // 登录
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log("res.code =" + res.code)
          // 服务器建立一个专门登录的算法。
          // 服务器的 wxjscode2sessionpost 在 https://api.weixin.qq.com/sns/jscode2session?appid=wxf130e68214a5c958&secret=d57e7eab6271fce883668caf57772837&js_code=JSCODE&grant_type=authorization_code
          // wxf130e68214a5c958
          // d57e7eab6271fce883668caf57772837
          // 061zt60004vF3M1oTU200V2HNZ1zt60Q
          wx.request({
            url: 'https://xuanfangyi.com/wxjscode2sessionpost/',
            method: 'post',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              // {"status":"OK","session_key":"cFc44uZGiTRNURk1BKkZgg==","openid":"oN2Ka5IRj8mOxoGra1o5LQOs9ho0"}
              console.log(res.data)
              wx.setStorage({
                key: "openid",
                data: res.data.openid
              })
              openid = res.data.openid
              that.globalData.openid = openid

            }, // success (res) {
            fail(res) {
              wx.showToast({
                title: "网络登录失败",
                icon: 'error',
                duration: 2000
              })
            }
          }) // wx.request({
        }
      })

    } // if (openid == "") {



    // 获得导航栏的高度数据 https://www.cnblogs.com/sese/p/9761713.html
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight,
          navTop = menuButtonObject.top, //胶囊按钮与顶部的距离
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; //导航高度
        this.globalData.navHeight = navHeight;
        this.globalData.navTop = navTop;
        this.globalData.windowHeight = res.windowHeight;



        this.globalData.screenWidth = res.screenWidth;
        this.globalData.screenHeight = res.screenHeight;

        this.globalData.platform = res.platform;
        this.globalData.brand = res.brand;
        this.globalData.model = res.model;
        console.log(res)
        


      },
      fail(err) {
        console.log(err);
      }
    })

    
  }, // onLaunch: function () {

  globalData: {
    
    apiVersion: "api/v1/"
  }
})